﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lidgren.Network;

namespace uam_fps_game.Network
{
    class Client
    {
        private NetClient client;

        private Client()
        {
            NetPeerConfiguration confing = new NetPeerConfiguration("Configure");
            client = new NetClient(confing);
        }

        public void Update()
        {
            ManageIncomingMessages();
        }

        public void ManageIncomingMessages()
        {
            NetIncomingMessage msg;
            while ((msg = client.ReadMessage()) != null)
            {
                switch (msg.MessageType)
                {
                    case NetIncomingMessageType.Data:
                    case NetIncomingMessageType.VerboseDebugMessage:
                    case NetIncomingMessageType.DebugMessage:
                    case NetIncomingMessageType.WarningMessage:
                    case NetIncomingMessageType.ErrorMessage:
                        //Console.WriteLine(msg.ReadString());
                        break;
                    default:
                        //Console.WriteLine("Unhandled type: " + msg.MessageType);
                        break;
                }
                client.Recycle(msg);
            }
        }

        public void Connect(string host, int port)
        {
            client.Start();
            NetOutgoingMessage hail = client.CreateMessage();
            hail.Write("Witamy na serwerze!");
            client.Connect(host, port, hail);
        }

        public void ShutDown()
        {
            client.Disconnect("Bye bye");
        }

        public void Send(string text)
        {
            NetOutgoingMessage om = client.CreateMessage(text);
            client.SendMessage(om, NetDeliveryMethod.ReliableOrdered);
        }
    }
}
